package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.UuidUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class XIZAIAttr implements AdapterAttr {


    public static void main(String[] args) {
        String wzry_dan = getAtta("sys_wzryDan");
        System.out.println(wzry_dan);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        String result = HttpRequest.get("https://www.xizai.com/trade/goodsApi/queryOpenCategory").execute().body();
        JSONObject resJson = JSONObject.parseObject(result);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("list"), JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("categoryId");
            String title = jsonObject.getString("categoryName");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        List<InterServerRes> interServerRes = new ArrayList<>();
        InterServerRes interServerRes1 = new InterServerRes();
        interServerRes1.setId("戏仔不匹配区服,直接保存即可!");
        interServerRes1.setName("戏仔不匹配区服,直接保存即可!");
        interServerRes.add(interServerRes1);
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        String result = HttpRequest.get("https://www.xizai.com/trade/goodsApi/queryOpenCategory").execute().body();
        JSONObject resJson = JSONObject.parseObject(result);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("list"), JSONObject.class);
        for(JSONObject jsonObject : jsonObjects){
            if(jsonObject.getString("categoryId").equals(gameId)){
                List<InterAttrRes> interAttrResList = new ArrayList<>();
                String gameFiled1 = jsonObject.getString("gameFiled");
                List<JSONObject> data = JSONObject.parseArray(gameFiled1, JSONObject.class);
                List<JSONObject> collect = new ArrayList<>();
                if (Objects.equals("1",type)){
                    collect = data.stream().filter(d -> StringUtils.isNotEmpty(d.getString("field_type"))?d.getString("field_type").equals("radio"):"1".equals("radio")).collect(Collectors.toList());
                }else if  (Objects.equals("2",type)){
                    collect = data.stream().filter(d -> StringUtils.isNotEmpty(d.getString("field_type"))?d.getString("field_type").equals("checkbox"):"1".equals("checkbox")).collect(Collectors.toList());
                }else if (Objects.equals("3",type)){
                    collect = data.stream().filter(d -> StringUtils.isNotEmpty(d.getString("field_type"))?d.getString("field_type").equals("input"):"1".equals("input")).collect(Collectors.toList());
                }
                for (JSONObject object : collect) {
                    InterAttrRes interAttrRes = new InterAttrRes();
                    interAttrRes.setUuid(UuidUtils.generateUuid());
                    interAttrRes.setId(object.getString("field_identification"));
                    String description = object.getString("description");
                    if(object.getString("description").contains("-")){
                        description = object.getString("description").split("-")[1];
                    }
                    interAttrRes.setName(description);
                    if(!type.equals("3")){
                        String field_dict_type = getAtta(object.getString("field_dict_type"));
                        String list = JSONObject.parseObject(field_dict_type).getString("list");
                        List<JSONObject> jsonArray = JSONObject.parseArray(list, JSONObject.class);
                        List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                        for(JSONObject jsonObject1 : jsonArray) {
                            InterAttrRes interAttrRes1 = new InterAttrRes();
                            interAttrRes1.setUuid(UuidUtils.generateUuid());
                            interAttrRes1.setId(jsonObject1.getString("value"));
                            interAttrRes1.setName(jsonObject1.getString("value"));
                            interAttrResList1.add(interAttrRes1);
                            interAttrRes.setInterServerRes(interAttrResList1);
                        }
                    }
                    interAttrResList.add(interAttrRes);
                }
                return interAttrResList;
            }
        }
        return null;
    }
    static String getAtta(String fieldDictType) {
        Map<String, Object> param = new HashMap<>();
        String url = "https://www.xizai.com/trade/goodsApi/queryGameField";
        param.put("field_dict_type", fieldDictType);
        String response = HttpUtils.doPostForm(url, param, null);
        return response;
    }
}